/* model */
import { BaseSelectUserErrorMessageEnum, BaseSelectUserWarnMessageEnum, SelectUserComponentUser } from '@src/component/common/BaseSelectUser/model'
import MsgModel from '@model/MsgModel'
/* store */
import {
  useBaseSelectUserStoreSearchUserListData,
  useBaseSelectUserStoreLoadingMap,
  useBaseSelectUserStoreFetchSearchUserList,
  useBaseSelectUserStoreFetchSearchMoreUserList,
  useBaseSelectUserStoreUserListMap,
  useBaseSelectUserStoreUserCountMap,
  useBaseSelectUserStoreIsSearchOrganizationHasMore,
  useBaseSelectUserStoreIsSearchSpHasMore
} from '@src/component/common/BaseSelectUser/store'
/* hooks */
import { useBaseSelectUserSearchMoreUserListParams } from '@src/component/common/BaseSelectUser/hooks/params'
import { useBaseSelectUserOptions } from '@src/component/common/BaseSelectUser/hooks/options'
/* util */
import { message } from '@src/util/message'
import { isFalse } from '@src/util/type'
import { setBaseSelectUserListChecked } from '@src/component/common/BaseSelectUser/util'
/* types */
import { 
  BaseSelectUserGetSearchUserListModel, 
  BaseSelectUserGetSearchUserListResult 
} from '@src/component/common/BaseSelectUser/types'
/* vue */
import { computed } from 'vue'
import Log from '@src/util/log'


function useSelectUserSearchMoreUserList() {
  
  const { searchMoreUserList } = useBaseSelectUserStoreUserListMap()
  const { searchMoreUserCount } = useBaseSelectUserStoreUserCountMap()
  
  // 搜索更多人员列表是否为空
  const isUserListEmpty = computed(() => {
    return searchMoreUserList.value.length == 0 || searchMoreUserCount.value == 0
  })
  
  // 搜索更多人员列表是否全部加载完成
  const isUserListAllLoaded = computed(() => {
    return searchMoreUserList.value.length >= searchMoreUserCount.value
  })
  
  // 搜索更多人员列表是否未全部加载完成
  const isUserListNotAllLoaded = computed(() => {
    return !isUserListAllLoaded.value
  })
  
  // 是否禁用加载更多人员状态
  const isLoadmoreDisabled = computed(() => {
    
    // 当前人员数量为空时，不禁用加载状态可以加载更多
    if (isUserListEmpty.value) {
      return false
    }
    
    // 全部加载人员后，禁用加载
    return isUserListAllLoaded.value
    
  })
  
  const isLoadmoreNotDisabled = computed(() => isFalse(isLoadmoreDisabled.value))
  
  return {
    isUserListEmpty,
    isUserListAllLoaded,
    isLoadmoreDisabled,
    isLoadmoreNotDisabled,
    isUserListNotAllLoaded
  }
  
}

/** 
 * @description 获取选人组件 搜索人员数据列表
*/
function useFetchSearchUserList() {
  
  const { setSearchUserListData } = useBaseSelectUserStoreSearchUserListData()
  const { setSearchLoading, clearSearchLoading } = useBaseSelectUserStoreLoadingMap()
  
  const { setIsSearchOrganizationHasMore } = useBaseSelectUserStoreIsSearchOrganizationHasMore()
  const { setIsSearchSpHasMore } = useBaseSelectUserStoreIsSearchSpHasMore()
  
  const { fetchSearchUserList: optionsFetchSearchUserList } = useBaseSelectUserOptions()
  
  const defaultFetchSearchUserList = useBaseSelectUserStoreFetchSearchUserList()
  
  const fetchSearchUserList = optionsFetchSearchUserList.value || defaultFetchSearchUserList
  
  const fetchSearchUserListImpl = (params: BaseSelectUserGetSearchUserListModel) => {
    
    setSearchLoading()
    
    return (
      fetchSearchUserList(params).then(result => {
        
        if (MsgModel.isSuccess(result)) {
          
          const userListData = result?.data || {} as BaseSelectUserGetSearchUserListResult
          // 组织架构人员列表
          const organizationUserList = (userListData?.organizationList || []) as SelectUserComponentUser[]
          // 服务商人员列表
          const serviceProviderUserList = (userListData?.spUserList || []) as SelectUserComponentUser[]
          
          setBaseSelectUserListChecked(organizationUserList)
          setBaseSelectUserListChecked(serviceProviderUserList)
          
          const searchUserListData = {
            organizationUserList,
            serviceProviderUserList
          }
          
          setSearchUserListData(searchUserListData)
          
          setIsSearchOrganizationHasMore(
            Boolean(userListData?.organizationHasMore)
          )
          
          setIsSearchSpHasMore(
            Boolean(userListData?.spHasMore)
          )
          
        } else {
          message.error(result?.message || BaseSelectUserErrorMessageEnum.SearchUserList)
        }
        
      }).finally(() => {
        clearSearchLoading()
      })
    )
  }
  
  return fetchSearchUserListImpl
}

/** 
 * @description 获取选人组件 搜索更多人员数据列表
*/
function useFetchSearchMoreUserList() {
  
  const { searchMoreUserList, setSearchMoreUserList } = useBaseSelectUserStoreUserListMap()
  const { setSearchMoreUserCount } = useBaseSelectUserStoreUserCountMap()
  
  const { setSearchLoading, clearSearchLoading } = useBaseSelectUserStoreLoadingMap()
  const { isLoadmoreDisabled, isUserListNotAllLoaded } = useSelectUserSearchMoreUserList()
  
  const { searchMoreUserListParams } = useBaseSelectUserSearchMoreUserListParams()
  
  const { fetchSearchMoreUserList: optionsFetchSearchMoreUserList } = useBaseSelectUserOptions()
  
  const defaultFetchSearchMoreUserList = useBaseSelectUserStoreFetchSearchMoreUserList()
  
  const fetchSearchMoreUserList = optionsFetchSearchMoreUserList.value || defaultFetchSearchMoreUserList
  
  const fetchSearchMoreUserListImpl = () => {
    
    if (isLoadmoreDisabled.value) {
      Log.warn(BaseSelectUserWarnMessageEnum.SearchMoreUserList, fetchSearchMoreUserListImpl.name)
      return Promise.resolve()
    }
    
    setSearchLoading()
    
    return (
      fetchSearchMoreUserList(searchMoreUserListParams.value).then(result => {
        
        if (MsgModel.isSuccess(result)) {
          
          const userCount = result?.data?.userTotalCount || 0
          let userList = (result?.data?.userList || []) as SelectUserComponentUser[]
          
          // 搜索更多人员列表没有全部加载完成，则合并新的数据
          if (isUserListNotAllLoaded.value) {
            userList = searchMoreUserList.value.concat(userList)
          }
          
          setBaseSelectUserListChecked(userList)
          
          setSearchMoreUserList(userList)
          setSearchMoreUserCount(userCount)
          
        } else {
          message.error(result?.message || BaseSelectUserErrorMessageEnum.SearchMoreUserList)
        }
        
      }).finally(() => {
        clearSearchLoading()
      })
    )
  }
  
  return fetchSearchMoreUserListImpl
}

export {
  useFetchSearchUserList,
  useFetchSearchMoreUserList,
  useSelectUserSearchMoreUserList
}
